<?php

namespace App\Http\Resources;

use App\Models\JobpositionModel;
use App\Models\PlazaModel;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Support\Collection;
use SuperEggs\DcatDistpicker\DcatDistpickerHelper;

class JobpositionSingListResource extends ResourceCollection
{

    /**
     * Transform the resource into an array.
     * 报名列表详情
     */
    public function toArray($request)
    {
        return [
            'item_list' => $this->collectionList($this->collection),
            'total' => $this->total(),
            'page' => $this->currentPage(),
            'count' => $this->count(),
            'is_next' => $this->hasMorePages(),
            'last_page' => $this->lastPage(),
        ];
    }

    private function collectionList(Collection $collection)
    {
        $list = [];
        foreach ($collection as $item) {
            $list[] = [
                'id' => $item->id,
                'job_position_id' => $item->job_position_id,
                'position_name' => $item->position_name,
                'salary' => (string)$item->salary,
                'salary_type' => JobpositionModel::SALARY_TYPE_LIST[$item->salary_type],
                'tags' => $item->tags,
                'label' => JobpositionModel::LABEL_LIST[$item->label] ?? '',
                'title_tag' => JobpositionModel::TITLE_TAG[$item->title_tag] ?? '',
                'work_location' => $item->work_location,
                'work_date' => $item->work_date,
                'company_name' => $item->company->company_name ? $item->company->company_name : '个体招聘',
                 
                'contact_person_avatar' => $item->company->contactPersonUrl(),
                'contact_person_name' => $item->company->contact_person_name ?? '',
                'region' => $this->region($item->region_city, $item->region_region),
                'created_at' => $item->created_at->format('m-d H:i'),
            ];
        }
        return $list;
    }

    private function region($region_city, $region_region)
    {
        if (!$region_region) {
            return '不限地点';
        }
        return DcatDistpickerHelper::getAreaName($region_city) . DcatDistpickerHelper::getAreaName($region_region);
    }
}
